package service

import (
	"fmt"
	"server_cluster/common/logger"
	"server_cluster/http/pkg/random"
	"strings"
)

func (s *Service) Login(username string, password string) (uint32, string, error) {
	account, err := s.d.QueryManagerByUsername(username)
	if err != nil {
		logger.Error("query account error: %v, name: %v", err, username)
		fmt.Println("account not exist")
		return 0, "", GmAccountNotExist
	}
	if password != account.Password {
		fmt.Println("password: ", password)
		fmt.Println("account password: ", account.Password)
		fmt.Println("password error")
		return 0, "", GmPasswordError
	}
	token := strings.ToLower(random.GetRandomStr(32))
	err = s.d.UpdateManagerToken(account, token)
	if err != nil {
		logger.Error("update account token to db error: %v", err)
		return 0, "", err
	}
	logger.Debug("account login ok, Id: %v", account.Id)
	return account.Id, token, nil
}
